package com.etlmaster.datax.plugins.tranformer;

import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.Map;

import com.dp.nebula.wormhole.common.interfaces.ILine;
import com.dp.nebula.wormhole.common.interfaces.IParam;
import com.etlmaster.executor.utils.JsonHelper;
import com.etlmaster.executor.utils.StringUtils;

/**
 * 行级处理器父类
 * @author wugf
 * @date 2016-04-29
 */
public abstract class LineTransProcessor {
	private int fieldIndex;
	private String changeCode;
	private IParam param;
	private String paramStr;
	public LineTransProcessor(Integer fieldIndex,String changeCode,String paramStr,IParam param){
		this.setFieldIndex(fieldIndex);
		this.setChangeCode(changeCode);
		this.setParam(param);
		this.setParamStr(paramStr);
		parseParam(paramStr);
	}
	public  abstract void transField(ILine line,IParam param);
	public abstract boolean isSkip();
	public int getFieldIndex() {
		return fieldIndex;
	}
	public void setFieldIndex(int fieldIndex) {
		this.fieldIndex = fieldIndex;
	}
	public String getChangeCode() {
		return changeCode;
	}
	public void setChangeCode(String changeCode) {
		this.changeCode = changeCode;
	}
	public IParam getParam() {
		return param;
	}
	public void setParam(IParam paramStr) {
		this.param = paramStr;
	}
	public String getParamStr() {
		return paramStr;
	}
	public void setParamStr(String paramStr) {
		this.paramStr = paramStr;
	}

	protected void parseParam(String paramStr){
		if(paramStr!=null && paramStr.length()>0){
			paramStr = paramStr.replaceAll("[\\t\\n]","");
			char firstChar = paramStr.charAt(0);
			if(firstChar=='[' || firstChar=='{'){//代表是json对象
				
				Map<String,Object> paramMap = (Map<String,Object>)JsonHelper.getInstance().read(paramStr);
				for (Iterator iterator = paramMap.entrySet().iterator(); iterator.hasNext();) {
					Class cls = this.getClass();
					Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iterator.next();
					
					String key = entry.getKey();
					Field field = null;
					while (field == null) {
						try {
							field = cls.getDeclaredField(key);//这个类的声明中定义的指定名称的属性，这个属性必须是在这个类的声明中定义，可以使私有和保护的,但是所有父类的属性都不会被返回
							if( field!=null ){
								field.setAccessible(true);
								if((field.getType() == Boolean.class || field.getType() == boolean.class)&& entry.getValue().toString().equalsIgnoreCase("true")){
									field.set(this, true);
								}else if((field.getType() == int.class || field.getType() == Integer.class) && (entry.getValue().getClass()==String.class && StringUtils.isNumberic(entry.getValue().toString()))){
									field.set(this, Integer.valueOf(entry.getValue().toString()));
								}else if((field.getType() == double.class || field.getType() == Double.class) && (entry.getValue().getClass()==String.class && StringUtils.isNumberic(entry.getValue().toString()))){
									field.set(this, Double.valueOf(entry.getValue().toString()));
								}else if((field.getType() == long.class || field.getType() == Long.class) && (entry.getValue().getClass()==String.class && StringUtils.isNumberic(entry.getValue().toString()))){
									field.set(this, Long.valueOf(entry.getValue().toString()));
								}else if(field.getType().isInstance(entry.getValue())){
									field.set(this, entry.getValue());
								}
							}
						} catch (NoSuchFieldException e) {
//						e.printStackTrace();
						} catch (SecurityException e) {
							e.printStackTrace();
						} catch (IllegalArgumentException e) {
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						}finally{
							cls = cls.getSuperclass();
							if(cls.getName().equals("java.lang.Object") ){
								break;
							}
						}
					}
				}
			}
		}
	}
}
